AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )



include('shared.lua')

local Ground = 1 + 0 + 2 + 8 + 32
local Energy_Increment = 20
local minNEU = 100
local I = 1
local X = 0
util.PrecacheSound( "ambient/levels/labs/electric_explosion4.wav" )
util.PrecacheSound( "ambient/levels/labs/electric_explosion3.wav" )
util.PrecacheSound( "ambient/levels/labs/electric_explosion1.wav" )

function ENT:Initialize()
	self.BaseClass.Initialize(self)
	self.Active = 0
	self.damaged = 0
	self.critical = 0
	self.hwcount = 0
	self.time = 0
	if not (WireAddon == nil) then
		self.WireDebugName = self.PrintName
		self.Inputs = Wire_CreateInputs(self.Entity, { "On" })
		self.Outputs = Wire_CreateOutputs(self.Entity, { "On", "Output" })
	end
	self.Entity:SetColor( 100, 12, 12, 255 )
end

function ENT:TurnOn()
	if (self.Active == 0 and RD_GetResourceAmount(self, "energy") > minNEU) then
		self.Active = 1
	
		if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "On", 1) end
		self:SetOOO(1)

	end
end

function ENT:TurnOff()
	if (self.Active == 1) then
		self.Active = 0
		if not (WireAddon == nil) then 
			Wire_TriggerOutput(self.Entity, "On", 0)
			Wire_TriggerOutput(self.Entity, "Output", 0)
		end
		self:SetOOO(0)
	end
end

function ENT:TriggerInput(iname, value)
	if (iname == "On") then
		self:SetActive(value)
	end
end

function ENT:OnRemove()
	self.BaseClass.OnRemove(self)
end

function ENT:Extract_Energy()
	local inc = Energy_Increment

	--the money shot!
	if (inc > 0) then
		if(RD_GetResourceAmount(self, "energy") >= minNEU) then
			local inc = math.random(2,5)*Energy_Increment
			RD_ConsumeResource(self, "energy", inc)
			RD_SupplyResource(self, "neutrions", (math.random(2,5)*(inc/5))) 
			
			else
			self:TurnOff()
		end
	end
	if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Output", inc) end

		
	
	
	
end

function ENT:Think()
	self.BaseClass.Think(self)
	
	
	
	if (self.Active == 1 and X >= 2) then
		self:Extract_Energy()
		X = 0
		end
	
	
	if (self.Active == 1) then
		
	
	
	local ang = self.Entity:GetAngles()
		local pos = (self.Entity:GetPos() + (ang:Up() * self.Entity:BoundingRadius()*2))
		local I = math.random(1, 10)
		if (I <= 1) then
			zapme((pos + (ang:Right() * 90)), 5)
			zapme((pos - (ang:Right() * 90)), 5)
			//self.Entity:EmitSound( "ambient/levels/labs/electric_explosion3.wav" )
			I = I +1
		elseif (I <= 5) then
			zapme((pos + (ang:Right() * 90)), 3)
			zapme((pos - (ang:Right() * 90)), 3)
			self.Entity:EmitSound( "ambient/levels/labs/electric_explosion4.wav" )
			I = I +1
		elseif (I <= 10) then
			zapme((pos + (ang:Right() * 90)), 2)
			zapme((pos - (ang:Right() * 90)), 2)
			//self.Entity:EmitSound( "ambient/levels/labs/electric_explosion1.wav" )
			I = I +1
		end
	end
	
	X = X +1
	self.Entity:NextThink(CurTime() + 0.5)
	return true
end

